
import os
from conans import ConanFile, CMake, tools
from conans import tools
import os
import sys
import os
sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path


class LibeventConan(ConanFile):
    name = "apr"
    version = "1.7.x"
    url = "https://gitee.com/apache/apr.git"
    description = "Apache Portable Runtime Library (APR)"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake_find_package"

    def source(self):
        print("source  cwd is", os.getcwd())
        print("sys path is", sys.path)

        my_file = Path("./pkgsrc")
        if my_file.exists():
            print("not need to update source")
        else:
            print("source  cwd is", os.getcwd())
            git = tools.Git(folder="./pkgsrc", verify_ssl=False)
            git.clone(self.url)

    def build(self):
        print("path " + self.package_folder + " is where the package built")
        try:
            os.remove(tools.get_env("CONAN_SH_PATH"))  # path是文件的路径，如果这个路径是一个文件夹，则会抛出OSError的错误，这时需用用rmdir()来删除
        except:
            print("")
        #print("cmake module is " + tools.get_env("CMAKE_MODULE_PATH"))
        print(tools.get_env("BUILD_ENV"))
        if tools.os_info.is_windows:
            print("current os windows")
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", bashcmd="cmake  -DCMAKE_MODULE_PATH=" + tools.unix_path(self.source_folder) + " -G\"MinGW Makefiles\" " + tools.unix_path(self.source_folder) + "//pkgsrc")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", bashcmd="cmake --build .")
        if tools.os_info.is_linux:
            print("current os linux")
            self.run("cmake  -DEVENT__DISABLE_MBEDTLS=ON -DCMAKE_MODULE_PATH=" + tools.unix_path(self.source_folder) + " -G\"Unix Makefiles\" " + tools.unix_path(self.source_folder) + "/pkgsrc",cwd="pkgsrc")
            self.run("cmake --build .",cwd="pkgsrc")


    def export(self):
        print("not need to export local file")

    def requirements(self):
        self.requires("OPENSSL/1.1.1")
    def package(self):
        self.copy("pkgsrc/include/*", dst="include", keep_path=True)
        self.copy("pkgsrc/lib/*", dst="lib", keep_path=False)


    def package_info(self):
        self.cpp_info.name = 'libevent'
        self.cpp_info.libs = ['libevent_core.a','libevent.a','libevent_openssl.a','libevent_extra.a']
        self.cpp_info.names["cmake_find_package"] = "libevent"
        self.cpp_info.names["cmake_find_package_multi"] = "libevent"

        if self.settings.os == "Windows":
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                self.cpp_info.names["cmake_find_package"] = "libevent"
                self.cpp_info.names["cmake_find_package_multi"] = "libevent"
                self.cpp_info.libdirs = ['pkgsrc/lib']  # Directories where libraries can be found
                self.cpp_info.libs = ['libevent_core.a','libevent.a','libevent_openssl.a','libevent_extra.a']
                self.cpp_info.buildirs = 'pkgsrc'

